<template>
  <div>

    <el-form :inline="true" :model="form" class="demo-form-inline"
    >

      <el-form-item label="用户名">
        <el-input v-model="form.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.phone" placeholder="手机号"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="query">查询</el-button>
        <el-button type="success" @click="dialogFormVisible = true">添加</el-button>
        <el-button type="success" @click="delBatch">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="tableData"
        ref="multipleTable"
        border
        @selection-change="handleSelectionChange"
        style="width: 100%">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="id"
          label="id"
          width="60">
      </el-table-column>
      <el-table-column
          prop="username"
          label="姓名"
          width="180">
      </el-table-column>
      <el-table-column
          prop="phone"
          label="手机号">
      </el-table-column>

      <el-table-column
          prop="created"
          label="日期">
      </el-table-column>

      <el-table-column
          prop="headImg"
          label="头像">
        <template v-slot="scope">
      <el-image
          style="width: 100px; height: 100px"
          :src="'http://127.0.0.1:8090'+scope.row.headImg"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
          prop="enabled"
          label="是否可用">

        <template v-slot="scope">
          <el-switch
              v-model="scope.row.enabled==1?true:false"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>

        </template>


      </el-table-column>

      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
          <el-button type="text" size="small">编辑</el-button>
          <el-button @click="openBindRole(scope.row)" type="text" size="small">绑定角色</el-button>
        </template>

      </el-table-column>

    </el-table>

    <el-pagination
        background
        layout="prev, pager, next"
        @current-change="goPage"
        :current-page="current"
        :page-size="size"
        :total="total"
    >
    </el-pagination>


    <!--增加-->
    <el-dialog title="增加用户" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="phone" :label-width="formLabelWidth">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>

          <el-form-item label="生日" :label-width="formLabelWidth" >
            <el-date-picker  placeholder="选择日期" value-format="yyyy-MM-dd" v-model="form.created" style="width: 100%;"></el-date-picker>
          </el-form-item>
       <el-form-item>
        <el-upload
            action="http://127.0.0.1:8090/user/upFile"
            list-type="picture-card"
            :on-success="uplocadsuccess"
            >
          <i class="el-icon-plus"></i>
        </el-upload>
       </el-form-item>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="form.headImg" alt="">
        </el-dialog>
        <el-form-item label="是否启用">
          <el-switch v-model="form.enabled" ></el-switch>
        </el-form-item>
      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <!--绑定角色-->
    <el-dialog title="绑定角色" :visible.sync="roleStatus">

      <el-checkbox-group v-model="selectRoleList">
        <el-checkbox :label="role.id" v-for="role in roleList">{{ role.name }}</el-checkbox>
      </el-checkbox-group>
      <div slot="footer" class="dialog-footer">
        <el-button @click="roleStatus = false">取 消</el-button>
        <el-button type="primary" @click="saveBindRole()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "UserView",
  data() {
    return {
      tableData: [],
      current: 1,//当前页码
      total: 0,
      size:3,
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        username: '',
        phone: '',
        headImg:'',
        created:'',
        enabled:true
      },
      userId: '',
      dialogFormVisible: false,
      roleStatus: false, //默认
      formLabelWidth: '120px',
      roleList: [],//角色数组
      selectRoleList: [], //当前已经选中的角色
      multipleSelection: []//批量删除时选中的行对象
    }

  }, methods: {

    uplocadsuccess(res) {
      this.from={}
      this.form.headImg = res.data;
      this.dialogVisible = false;


  },
  list() {
     this.axios.get(this.$baseUrl + 'user/list', {
      params: {
        current: this.current, size: this.size, username: this.username, phone: this.phone
      }
    }).then(res => {
      this.tableData = res.data.data.records;
      this.total = res.data.data.total;
       console.log(this.total);


    })
  }
  , goPage(current) {
    this.current = current;
    this.list()
  }
  , query() {
    this.list()
  }, save() {
    this.axios.post(this.$baseUrl + 'user/saveOrUpdate', this.form).then(res => {
      this.$message(res.data.msg);
      this.dialogFormVisible = false
      this.list();
    })
  },
  openBindRole(row) {//打开绑定对话框
    this.listRole(); //1 获取所有角色
    this.getRoleIdsByUserId(row.id)//2 获取当前用户的角色
    this.userId = row.id//3 要绑定的用户

  }, getRoleIdsByUserId(uid) {
    this.roleStatus = true
    this.axios.get(this.$baseUrl + 'role/getRoleIdsByUserId', {params: {uid: uid}}).then(res => {
      this.selectRoleList = res.data.data;
      //   console.log(this.selectRoleList)
    })
  }, saveBindRole() {
    this.axios.post(this.$baseUrl + 'user/bindRole', {id: this.userId, roleIds: this.selectRoleList}).then(res => {
      if (res.data.msg) {
        this.$message("绑定成功");
        this.roleStatus = false;
      }
    })
    console.log(this.selectRoleList)

  }
  ,
  listRole() {
    this.roleStatus = true;//让对话框打开
    this.axios.get(this.$baseUrl + 'role/list').then(res => {
      this.roleList = res.data.data;
    })
  },
  handleSelectionChange(val) {//获取所有选中的userid
    this.multipleSelection = val;

  }, delBatch: function () {//批量删除
    if (this.multipleSelection.length == 0) {
      this.$message('请至少选中一个');
      return;
    }

    let ids = this.multipleSelection.map(x => x.id).join(",");//把选中的行对象的ID 提取出数组

    this.axios.get(this.$baseUrl + 'user/delBatch', {params: {ids: ids}}).then(res => {
      this.$message(res.data.msg)
      this.list();
    })

  }
}
,
created()
{
  this.list()
}
}
</script>

<style scoped>

</style>
